<?php
include '../Util/phpqrcode.php';
include_once '../Util/Common.php';
require_once __DIR__ . './../Util/Utils.php';

use UtilMethod\Utils;

$utils = new Utils();

$post_data = [
    'account' => ACCOUNT,
    'orderCode' => 'xy_ChangeRate'
];

$msgDate = [
    'channel_code' => 'ALIPAY',
    'password' => PASSWORD,
    'wx_rate' => 0.004,
    'ali_rate' => 0.004,
    'cbzid' => '820160803195641770987'
];

$post_data['msg'] = base64_encode(json_encode($msgDate, JSON_UNESCAPED_SLASHES));

$priKeyFilePath = PRIVATEKEYPATH . $post_data['account'] . "_private_key.pem";
$sign = $utils::rsaDataSign($post_data['msg'], $priKeyFilePath); //RSA签名


$data = base64_encode(json_encode($post_data, JSON_UNESCAPED_SLASHES));
$decrypt = $utils::rsaPublicEncrypt($data, PUBLICKEYPATH); //RSA公钥加密

$send_data = [
    'data' => $decrypt,
    'signature' => $sign
];

$res = $utils::send_post1(KUBEI, json_encode($send_data, JSON_UNESCAPED_SLASHES));
$res = json_decode($res, true);
$res_data = $res['data'];
$res_sign = $res['signature'];

$original = $utils::rsaPrivateDecrypt(base64_decode($res_data), $priKeyFilePath); //RSA私钥解密
$original = json_decode($original, true);
$res_msg = json_decode($original['msg'], true);

//验证签名
$valid = $utils::isValid(json_encode($res_msg, JSON_UNESCAPED_SLASHES), base64_decode($res_sign), PUBLICKEYPATH);

if ("000000" === $res_msg['respCode']) {
   print("修改费率成功.");
} else {
    print("修改费率失败</br>返回码:" . $res_msg['respCode'] . "</br>失败原因:" . $res_msg['respInfo'] . "<br/>");
    var_dump($res_msg);
}

